const Room = require('../database/roomDao');
const Order = require('../database/orderDao');
const expree = require('express')
const router = expree.Router()
const authMiddleware = require('../middleware/auth')
// 筛选会议室
router.post('/filter', authMiddleware(['admin']) ,async (req,res) => {
  try {
      const { type, seats, equipment, start_time, end_time } = req.body;
      
      if (!start_time || !end_time) {
        return res.status(400).json({ error: '开始时间和结束时间必填' });
      }
      
      const rooms = await Room.filter({
        type,
        seats,
        equipment,
        startTime: start_time,
        endTime: end_time
      });
      
      res.json(rooms);
    } catch (error) {
      res.status(500).json({ error: '服务器错误' });
    }
})
router.get('/getAllRoom', async (req,res) => {
    try {
      const rooms = await Room.findAll();
      res.json(rooms);
    } catch (error) {
      res.status(500).json({ error: '服务器错误' });
    }
})
// 创建会议室
router.post('/create', async (req,res) => {
    try {
      const { room_name, type, area, seats, equipment, price,userId } = req.body;
      const roomId = await Room.create({
        room_name,
        type,
        area,
        seats,
        equipment,
        price,
        userId
      });
      
      res.json({ room_id: roomId, message: '会议室创建成功' });
    } catch (error) {
      console.log(error)
      res.status(500).json({ error: '服务器错误' });
    }
})
// 更新会议室
router.put('/:room_id/update',authMiddleware(['admin']), async (req,res) => {
    try {
      const roomId = req.params.room_id;
      const updateData = req.body;
      await Room.update(roomId, updateData);
      res.json({ message: '会议室更新成功' });
    } catch (error) {
      // console.log(error)
      res.status(500).json({ error: '服务器错误' });
    }
})
// 删除会议室
router.delete('/:room_id/delete',async (req,res) => {
    try {
      const roomId = req.params.room_id;
      await Room.delete(roomId);
      res.json({ message: '会议室删除成功' });
    } catch (error) {
      res.status(500).json({ error: '服务器错误' });
    }
})
// 更新会议室状态
router.put('/:room_id/status', authMiddleware(['employee', 'admin']), async (req,res) => {
    try {
      const roomId = req.params.room_id;
      const { status } = req.body;
      
      await Room.updateStatus(roomId, status);
      res.json({ message: '会议室状态更新成功' });
    } catch (error) {
      res.status(500).json({ error: '服务器错误' });
    }
})

module.exports = router